Supporting the changes of value or state is, again, very easy.
In the kEventControlDraw code above, the same thing (a circle) is drawn no matter what the value of the HIView is. We can just replace the line:
CGContextAddArc(context, cx, cy, minDim, 0, 2 * pi, true);
with:
// having some fun with geometric shapes based on the value of the custom view
UInt32 i, n = GetControl32BitValue(myData->view);
switch (n)
{
case 0: CGContextAddArc(context, cx, cy, minDim, 0, 2 * pi, true); break;
case 1: CGContextAddEllipseInRect(context, CGRectInset(bounds, bounds.size.width * 0.4, 0)); break;
default:
{
float deltangle = pi / n, angle = 0, r = minDim / 2;
CGContextMoveToPoint(context, cx + minDim, cy);
for (i = 0; i < n; i++)
{
angle += deltangle;
CGContextAddLineToPoint(context, cx + r * cos(angle), cy + r * sin(angle));
angle += deltangle;
CGContextAddLineToPoint(context, cx + minDim * cos(angle), cy + minDim * sin(angle));
}
CGContextAddLineToPoint(context, cx + minDim, cy);
}
}
And we will get the following display for value 17:
For the states (active, enable, hilite), we just change the color settings:
// setting our colors according to state: IsControlEnabled, IsControlActive, IsControlHilited
if (!IsControlEnabled(myData->view))
{
CGContextSetRGBFillColor(context, 0.3, 0.3, 0.3, 0.8);
CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 0.8);
}
else if (!IsControlActive(myData->view))
{
CGContextSetRGBFillColor(context, 0.7, 0.7, 0.7, 0.8);
CGContextSetRGBStrokeColor(context, 0.8, 0.8, 0.8, 0.8);
}
else if (!IsControlHilited(myData->view))
{
CGContextSetRGBFillColor(context, 1, 0, 0, 0.8);
CGContextSetRGBStrokeColor(context, 0, 0, 1, 0.8);
}
else
{
CGContextSetRGBFillColor(context, 0.7, 0, 0, 0.8);
CGContextSetRGBStrokeColor(context, 0, 0, 0.7, 0.8);
}
And we will get the following display when the HIView is disabled:
The last thing we have to do is to generate a redraw event whenever the custom HIView’s value or state changes.
To be alerted of any change of the value or state, we just intercept 2 more Carbon Events: kEventControlValueFieldChanged and kEventControlHiliteChanged. So we change the static EventTypeSpec kFactoryEvents[] declaration in the function GetHICustomViewClass to:
static EventTypeSpec kFactoryEvents[] =
{
{ kEventClassHIObject, kEventHIObjectConstruct },
{ kEventClassHIObject, kEventHIObjectDestruct },
{ kEventClassControl, kEventControlDraw },
{ kEventClassControl, kEventControlValueFieldChanged },
{ kEventClassControl, kEventControlHiliteChanged }
};
And we add 2 more cases in our case kEventClassControl switch in the function Internal_HICustomViewHandler:
case kEventControlValueFieldChanged:
case kEventControlHiliteChanged:
{
// just asking for a redraw
HIViewSetNeedsDisplay(myData->view, true);
break;
}
That’s it for the support of value and state, it’s all in the kEventControlDraw handler which is really the center piece of your custom HIView. You can test the custom HIView for this step by using the project located in the folder “2_Value_State”, and you will see that now, all the Tester controls from the “SetNeedsDisplay true” push button to the “Active” check box have an immediate effect on the display of the custom HIView.